package com.retch.wenchengmao.recodeauthutils;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.retch.wenchengmao.recodeauthutils.asyn.AsynActivity;
import com.retch.wenchengmao.recodeauthutils.bean.AuthUtil;
import com.retch.wenchengmao.recodeauthutils.httpUrlConnection.HttpUrlConnectionActivity;
import com.retch.wenchengmao.recodeauthutils.listview.ListViewActivity;

//测试类
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button fragment,tabLayout,asynText,listView,httpConnection;
    boolean getPermission;
    boolean getPermission2;
    String permission=Manifest.permission.READ_PHONE_STATE;
    String permissions[]={Manifest.permission.PROCESS_OUTGOING_CALLS ,
    Manifest.permission.ACCESS_FINE_LOCATION};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AuthUtil.getPermission(MainActivity.this, permission,1);
//        AuthUtil.getMultiPermission(MainActivity.this,permissions,2);
        AuthUtil.getPermissions(MainActivity.this,permissions);
        initView();


        if (getPermission2==true)
        {
            Log.d("权限","获取权限成功!");
        }
    }

    public void initView()
    {
        fragment= (Button) findViewById(R.id.fragment);
        fragment.setOnClickListener(this);
        tabLayout= (Button) findViewById(R.id.tab_layout);
        tabLayout.setOnClickListener(this);
        asynText= (Button) findViewById(R.id.asyn_text);
        asynText.setOnClickListener(this);
        listView=(Button)findViewById(R.id.listView);
        listView.setOnClickListener(this);
        httpConnection=(Button)findViewById(R.id.httpUtlConnection);
        httpConnection.setOnClickListener(this);
    }

    /**
     * 请求权限的回调
     *
     * 参数1：requestCode-->是requestPermissions()方法传递过来的请求码。
     * 参数2：permissions-->是requestPermissions()方法传递过来的需要申请权限
     * 参数3：grantResults-->是申请权限后，系统返回的结果，PackageManager.PERMISSION_GRANTED表示授权成功，PackageManager.PERMISSION_DENIED表示授权失败。
     * grantResults和permissions是一一对应的
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1 && grantResults.length > 0) {
            //是否授权，可以根据permission作为标记
            getPermission=true;
        }
        if (requestCode == 2) {
            if (verifyPermissions(grantResults)==true) {      //没有授权
                Toast.makeText(MainActivity.this,"授权成功！",Toast.LENGTH_LONG).show();
            }
            else
                Toast.makeText(MainActivity.this,"授权失败!",Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.fragment:
                startActivity(new Intent(MainActivity.this,MainBottomFragmentActivity.class));
                break;

            case R.id.tab_layout:
                startActivity(new Intent(MainActivity.this,MainTopFragmentActivity.class));
                break;

            case R.id.asyn_text:
                startActivity(new Intent(MainActivity.this, AsynActivity.class));
                break;

            case R.id.listView:
                startActivity(new Intent(MainActivity.this, ListViewActivity.class));
                break;

            case R.id.httpUtlConnection:
                startActivity(new Intent(MainActivity.this, HttpUrlConnectionActivity.class));
                break;

            default:
                break;
        }
    }

    /**
     * 检测是否说有的权限都已经授权
     *
     * @param grantResults
     * @return
     * @since 2.5.0
     */
    private boolean verifyPermissions(int[] grantResults) {
        for (int result : grantResults) {
            if (result == PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        }
        return false;
    }
}
